package GUI;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import Background.Contestant;
import Background.Lists;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Justin Sorrell
 * @version 23 January, 2013
 * 
 * This class will create the main page of the program.
 *
 */
@SuppressWarnings("serial")
public class Main_GUI extends JFrame implements WindowListener{

	/**
	 * A dimension field for use in setting the size of the frame.
	 */
	
	private static final Dimension MY_SIZE = new Dimension(1000, 600);

	/**
	 * This field is for the Border Layout that contains all my buttons.
	 */
	
	private static Container MY_PANEL;
	
	/**
	 * This field creates a flow layout which holds the buttons in the left side of the frame.
	 */

	private static Container MY_GRID_LEFT;
	

	/**
	 * A button to display the registration option.
	 */
	
	public static JButton Registration;
	
	/**
	 * A button to display the sign-in option.
	 */
	
	public static JButton Sign_in;
	
	/**
	 * A button to display the sign-out option.
	 */
	
	public static JButton Sign_out;
	
	/**
	 * A button to display the list of entrants when pressed.
	 */
	
	public static JButton Contestant_List;

	/**
	 * A button to display the list of judges when pressed.
	 */

	public static JButton Judge_List;
	
	/**
	 * A button to display the list of entrants when pressed.
	 */
	
	public static JButton Entry_List;

	/**
	 * A button to display the winners of the contest.
	 */
	
	public static JButton Contest_Winners;
	
	/**
	 * A button for the judges to select the contest winners.
	 */
	
	public static JButton Select_Winners = new JButton("Select Winners");

	/**
	 * A button for the attendees to submit an entry.
	 */
	
	public static final JButton Entry = new JButton("Submit an entry");
	
	
	/**
	 * A boolean to indicate if the user is a contestant.
	 */
	
	public boolean isContestant = false;
	
	/**
	 * A list to hold the strings of the user's information.
	 */
	
	public ArrayList<String> my_list;
	
	/**
	 * A button for the user to edit their information.
	 */
	
	public static JButton Edit_Button = new JButton("Edit My Info");
	
	/**
	 * The register button creates a new RegistrationGUI.
	 */
	
	public static RegistrationGUI register;
	
	/**
	 * The sign-in button creates a new SignInGUI.
	 */
	
	public static SignInGUI sign_in;
	
	/**
	 * A string to hold the name of the user.
	 */
	
	private static String users_name;
	
	BufferedImage myPicture;
	
	JLabel picLabel;
	
	/**
	 * Constructs the frame, giving it the title.
	 */

	public Main_GUI()
	  {
	    super("JUST BE WEAVE");
	    register = new RegistrationGUI();
	    MY_PANEL = new JPanel(new BorderLayout());
		MY_GRID_LEFT = new JPanel(new GridLayout(7,1));
		Registration = new JButton("Registration");
		Sign_in = new JButton("Sign-in");
		Sign_out = new JButton("Sign-out");
		Contestant_List = new JButton("Contestant List");
		Judge_List = new JButton("Judge List");
		Entry_List = new JButton("List of Entries");
		Contest_Winners = new JButton("Contest Winners");
		Select_Winners = new JButton("Select Winners");
		
	  }

	/**
	 * The main method, invokes the JustBeWeave GUI. Command line arguments are ignored.
	 * 	 * @param the_args Command line arguments.
	 * @throws IOException
	 */

	public static void main(final String[] the_args) throws IOException {
		final Main_GUI gui = new Main_GUI();
		gui.start();
		gui.setLocationRelativeTo(null);
	}
	
	/**
	  * Creates some buttons.
	  */

	private void createButtons() {

		Registration.addActionListener(new RegisterListener());
		Sign_in.addActionListener(new SignInListener(this));
		Sign_out.addActionListener(new SignOutListener());
		Contestant_List.addActionListener(new ContestantListListener());
		Judge_List.addActionListener(new JudgeListListener());
		Entry_List.addActionListener(new EntryListListener());
		Select_Winners.addActionListener(new SelectWinnersListener());
		Contest_Winners.addActionListener(new ContestWinnersListener());
		Entry.addActionListener(new EntryListener());
		add(MY_GRID_LEFT, BorderLayout.WEST);
		MY_GRID_LEFT.add(Sign_in);
		MY_GRID_LEFT.add(Registration);
		MY_GRID_LEFT.add(Contest_Winners);
	    my_list = new ArrayList<String>();
		
	}

	public void addEvent() {
		Background.Event the_event = new Background.Event();
		the_event.setName("Semi-Annual Weaving Contest");
		the_event.setDate(2013, 21, 7);
		the_event.setLocation("Everett Event Center");
		the_event.setType("Custon Design Competition");
		the_event.makeEvent(MY_PANEL);
	}

	/**
	 * Method that starts the program and sets up the GUI.
	 * @throws IOException 
	 */

	public void start() throws IOException {
//		try {
//			myPicture = ImageIO.read(new File("src/Background/JustBeWeave.bmp"));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		try {
		myPicture = ImageIO.read(new File("JustBeWeave.bmp"));
	} catch (IOException e) {
		e.printStackTrace();
	}
		Contestant currentContestant = null;
		
		picLabel = new JLabel(new ImageIcon( myPicture ));
		//picLabel.setBackground(Color.BLACK);
		MY_PANEL.add(picLabel, BorderLayout.CENTER);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
		createButtons();
		addEvent();
		add(MY_PANEL);
		setPreferredSize(MY_SIZE);
		Lists.readData();
		ArrayList<Contestant> contestantList = Lists.getContestantList();
		
		for (Contestant c : contestantList){//get the last contestant in the list.
			currentContestant = c;
			//Lists.addWinner(c);//everybody is a winner!
		}
		RegistrationGUI.registration_number = currentContestant.getRegistrationNumber() + 1;
		
		
		repaint();
		pack();

	
	}

	
	/**
	 * Returns a reference to the RegistrationGUI.
	 * @return register The RegistrationGUI
	 */
	
	public static RegistrationGUI getRegistrationGUI(){
		return register;
		
	}
	
	/**
	 * Returns a reference to the SignInGUI.
	 * @return register The SignINGUI
	 */
	
	public static SignInGUI getSignInGUI(){
		return sign_in;
		
	}

	
	
	/**
	 * A method that can be called to add the sign-out button to the frame upon signing in. 
	 */
	
	public static void createSignOutButton(final String name){
		MY_GRID_LEFT.add(Sign_out);
		users_name = name;
	}

	/**
	 * A method that can be called to add the sign-out button to the frame upon signing in. 
	 */
	
	public static void createEntryButton(){
		MY_GRID_LEFT.add(Entry);
	}

	
	/**
	 * A method that can be called to add the contestant list button to the frame. Visible 
	 * when signed in as an organizer.
	 */
	
	public static void createContestantListButton(){
		MY_GRID_LEFT.add(Contestant_List);
	}
	
	/**
	 * A method that can be called to add the judge button to the frame. Visible 
	 * when signed in as an organizer.
	 */
	
	public static void createJudgeListButton(){
		MY_GRID_LEFT.add(Judge_List);
	}

	
	/**
	 * A method that can be called to add the entry list button to the frame. Visible 
	 * when signed in as a judge.
	 */
	
	public static void createEntryListButton(){
		MY_GRID_LEFT.add(Entry_List);
	}
	
	/**
	 * A method that can be called to add the sign-out button to the frame upon signing in. 
	 */
	
	public static void createSelectWinnersButton(){
		MY_GRID_LEFT.add(Select_Winners);
	}	
	
	/**
	 * Inner class to bring up the window to start the registration process when
	 * the button is pressed.
	 * @author Justin Sorrell
	 */

	class RegisterListener implements ActionListener {

		boolean isBuilt = false;

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			
			register = new RegistrationGUI();
			register.start();
			register.setLocationRelativeTo(null);
			repaint();
		}
	}
	
	/**
	 * Inner class to bring up the window to start the sign in process when the button is
	 * pressed.
	 * @author Chris Petcher
	 */

	class SignInListener implements ActionListener {


		private Main_GUI gui;
		
		/**
		 * Constructor
		 * @param the_gui
		 */
		
		public SignInListener(final Main_GUI the_gui){
			gui = the_gui;
		}

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 * 
		 */

		
		@Override
		public void actionPerformed(final ActionEvent the_event) {

				sign_in = new SignInGUI(gui);
				sign_in.start();
				sign_in.setLocationRelativeTo(null);
				repaint();
		}
	}

	/**
	 * Inner class to sign out the current user when the button is pressed.
	 * @author Chris Petcher
	 */

	class SignOutListener implements ActionListener {


		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {

			JOptionPane.showMessageDialog(null, "Thanks for using our product, " + 
					users_name + ". Signing you out.");
			Sign_in.setEnabled(true);
			Sign_in.setText("Sign-in");
			Registration.setEnabled(true);
			MY_GRID_LEFT.remove(Sign_out);
			MY_GRID_LEFT.remove(Contestant_List);
			MY_GRID_LEFT.remove(Judge_List);
			MY_GRID_LEFT.remove(Entry_List);
			MY_GRID_LEFT.remove(Entry);
			MY_GRID_LEFT.remove(Select_Winners);
			MY_GRID_LEFT.remove(Edit_Button);
			repaint();
		}
	}

	/**
	 * Inner class for the judges to select winners.
	 * @author Chris Petcher
	 */

	class SelectWinnersListener implements ActionListener {


		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			SelectWinnersGUI selection = new SelectWinnersGUI();
			selection.start();
			selection.setLocationRelativeTo(null);
			repaint();

		}
	}
	
	
	/**
	 * Inner class to display the list of contestants for the organizer.
	 * @author Chris Petcher
	 */

	class ContestantListListener implements ActionListener {

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			ListGUI list = new ListGUI();
			list.SetChoice(1);
			list.start();
			list.setLocationRelativeTo(null);
			repaint();

		}
	}

	/**
	 * Inner class to display the list of judges for the organizer.
	 * @author Chris Petcher
	 */

	class JudgeListListener implements ActionListener {

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			ListGUI list = new ListGUI();
			list.SetChoice(2);
			list.start();
			list.setLocationRelativeTo(null);
			repaint();

		}
	}

	
	/**
	 * Inner class to display the list of entries for the judges.
	 * @author Chris Petcher
	 */

	class EntryListListener implements ActionListener {


		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			ListGUI list = new ListGUI();
			list.SetChoice(3);
			list.start();
			list.setLocationRelativeTo(null);
			repaint();

		}
	}
	
	/**
	 * Inner class to Display the list of contest winners.
	 * @author Chris Petcher
	 */

	class ContestWinnersListener implements ActionListener {


		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			ListGUI list = new ListGUI();
			list.SetChoice(4);
			list.start();
			list.setLocationRelativeTo(null);
			repaint();
		}
	}

	/**
	 * Inner class for the judges to select winners.
	 * @author Chris Petcher
	 */

	class EntryListener implements ActionListener {

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			SubmitEntryGUI submission = new SubmitEntryGUI();
			submission.start();
			submission.setLocationRelativeTo(null);
			repaint();
			

		}
	}

	/**
	 * Inner class to set the current event.
	 * @author Chris Petcher
	 */

	class EventsListener implements ActionListener {

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			repaint();

		}
	}

	
	
	/**
	 * Creates the button to edit your information.
	 * @param c The contestant to edit.
	 */
	
	public static void createEditButton(final Contestant c){
		Edit_Button = new JButton("Edit My Info");
		MY_GRID_LEFT.add(Edit_Button);
		class Edit implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				register = new RegistrationGUI();
				register.start();
				register.setLocationRelativeTo(null);
				register.displayEdit(c);
			}
			
		}
		Edit_Button.addActionListener(new Edit());
		
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		Lists.saveData();
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		Lists.saveData();
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

}